package task;

import utils.DateUtils;

import javax.swing.*;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class UpdateLabelTask {

    private final JLabel timeValueLabel; // 假设这是你的JLabel实例
    private ScheduledExecutorService scheduler;
    private static Date COLLECT_TIME;

    public UpdateLabelTask(JLabel label) {
        this.timeValueLabel = label;
        this.scheduler = Executors.newSingleThreadScheduledExecutor();
        COLLECT_TIME = new Date();
    }

    public void startUpdating() {
        scheduler.scheduleAtFixedRate(this::updateLabel, 0, 1, TimeUnit.SECONDS);
    }

    private void updateLabel() {
        timeValueLabel.setText(DateUtils.getDatePoor(new Date(), COLLECT_TIME, " 小时 ", " 分钟 ") + " 秒 ");
    }

    public void stopUpdating() {
        if (scheduler != null && !scheduler.isShutdown()) {
            scheduler.shutdown();
        }
    }
}